import re
import requests
from requests import exceptions
from lxml import etree


def yuming_cx(cx):
    xingxi = Yuming(cx)
    pass


class Yuming:
    def __init__(self, yuming):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0',
            'Cookie': '__utmc=30149280; __utmc=223695111; bid=Cqkkd_P-tmQ;'
        }
        url = 'https://seo.chinaz.com/' + yuming
        url_2 = 'http://www.' + yuming
        try:
            request = requests.get(headers=headers, url=url, allow_redirects=False, timeout=5)
        except exceptions.RequestException as e:
            w = str(e)
            c = w.find('port=', 0)
            print('查询异常，状态码：{}'.format(w[c:c + 8:1]))
        else:
            request.encoding = 'utf-8'
            txt = etree.HTML(request.text)
            zhucejigou = txt.xpath('/html/body/div[4]/table/tbody/tr[3]/td[2]/div[1]/span[1]/i/text()')
            zhuceem = txt.xpath('/html/body/div[4]/table/tbody/tr[3]/td[2]/div[1]/span[2]/a/i/text()')
            yumingage = txt.xpath('/html/body/div[4]/table/tbody/tr[3]/td[2]/div[2]/span/a/i/text()')
            beianhao = txt.xpath('/html/body/div[4]/table/tbody/tr[4]/td[2]/span[1]/i/a/text()')
            name = txt.xpath('/html/body/div[4]/table/tbody/tr[4]/td[2]/span[2]/i/text()')
            xingzhi = txt.xpath('/html/body/div[4]/table/tbody/tr[4]/td[2]/span[3]/i/text()')
            shenghe = txt.xpath('/html/body/div[4]/table/tbody/tr[4]/td[2]/span[4]/i/text()')
            ipdz = txt.xpath('/html/body/div[4]/table/tbody/tr[5]/td[2]/div[1]/span[1]/i/a/text()')
            try:
                wangzhi = requests.get(headers=headers, url=url_2, allow_redirects=False, timeout=5)
            except exceptions.RequestException as e:
                w = str(e)
                c = w.find('port=', 0)
                print('目标网站请求异常，状态码：{}'.format(w[c:c + 8:1]))
            else:
                sudu = '响应速度：' + str(wangzhi.elapsed.total_seconds())
                zhucejigou = '注册机构：' + ''.join(zhucejigou).replace('\r\n', '').replace(' ', '')
                zhuceem = '注册邮箱：' + ''.join(zhuceem).replace('\r\n', '').replace(' ', '')
                yumingage = '域名年龄：' + ''.join(yumingage).replace('\r\n', '').replace(' ', '')
                beianhao = '备案号码：' + ''.join(beianhao)
                name = '网站名称：' + ''.join(name).replace('\r\n', '').replace(' ', '')
                xingzhi = '网站性质：' + ''.join(xingzhi)
                shenghe = '审核时间：' + ''.join(shenghe)
                ipdz = 'IP地址：' + ''.join(ipdz)
                print_yum = '{0:<70}{1:<70}\n{2:<70}{3:<70}\n{4:<70}{5:<70}\n{6:<70}{7:<70}\n{8:<70}'
                print(print_yum.format(zhucejigou, name, sudu, beianhao, zhuceem, xingzhi, shenghe, ipdz, yumingage,
                                       chr(12288)))
                print(txt)



if __name__ == '__main__':
    yuming_or_ip = input('输入域名或者ip：')
    if re.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
                yuming_or_ip) or (
            r'^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$', yuming_or_ip):
        while True:
            text_1 = input('1、查询域名的所有信息\n2、查询IP所有信息\n输入对应序号：')
            if text_1 == '1':
                yuming_cx(yuming_or_ip)
